import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86156
 * Date: 2023-04-07
 * Time: 16:20
 */
public class StudentTest {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        // 初始化数组
        Student stu1 = new Student(1, "张三", 18);
        Student stu2 = new Student(2, "李四", 19);
        Student stu3 = new Student(3, "王五", 20);
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;


        Student stu = insertStudent();
        boolean flag = contains(arr, stu.getId());

        if(flag){
            System.out.println("该学生已经存在,请重新输入:");
        }else{
            int count = getCount(arr);
            if(count == arr.length){
                // 数组已满,创建一个新的数组
                Student[] newArr = new Student[arr.length + 1];
                for (int i = 0; i < arr.length; i++) {
                    newArr[i] = arr[i];
                }
                newArr[newArr.length-1] = stu;
                printStudent(newArr);
            }else{
                arr[count] = stu;
                printStudent(arr);
            }
        }
    }
    // 1.录入
    public static Student insertStudent(){
        Scanner scanner = new Scanner(System.in);
        Student stu = new Student();
        System.out.println("请输入学生学号");
        stu.setId(scanner.nextInt());
        System.out.println("请输入学生姓名");
        stu.setName(scanner.next());
        System.out.println("请输入学生年龄");
        stu.setAge(scanner.nextInt());
        return stu;
    }

    // 2.唯一性判断
    public static boolean contains(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                if (id == arr[i].getId()) {
                    return true;
                }
            }
        }
        return false;
    }

    // 判断数组是否已满
    public static int getCount(Student[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null){
              count++;
            }
        }
        return count;
    }
    // 4.遍历
    public static void printStudent(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println("学生学号为" + arr[i].getId() + " 学生姓名为" + arr[i].getName() + " 学生年龄为" + arr[i].getAge());
            }
        }
    }
}

